//
//  FirstHeadCollectionReusableView.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/12.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "FirstHeadCollectionReusableView.h"


@implementation FirstHeadCollectionReusableView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
       // self.backgroundColor=[UIColor whiteColor];
        _cycleScrollerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, MainW, MainW*0.4) delegate:self placeholderImage:[UIImage imageNamed:@"banner_bj"]];
        _cycleScrollerView.tag=100;
    _cycleScrollerView.bannerImageViewContentMode=UIViewContentModeScaleAspectFill;
        
        //    for (scrollViewModel * model in self.scrollViewArray) {
        //        NSString * pic = [NSString stringWithFormat:@"%@%@",baseImgUrl,model.banner_img];
        //        [array addObject:pic];
        //    }
        
        //_cycleScrollerView.imageURLStringsGroup = array;  // 网络图片
        //_cycleScrollerView.localizationImageNamesGroup =array ; // 本地图片
        _cycleScrollerView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _cycleScrollerView.autoScrollTimeInterval =3.;
        [self addSubview:_cycleScrollerView];
        UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, MainW*0.4, MainW, 200)];
        view.backgroundColor=[UIColor whiteColor];
        [self addSubview:view];
        int columnCount=3;
        //没个格子的宽度和高度
        CGFloat appW=50;
        CGFloat appH=50;
        //计算间隙
        
        CGFloat appMargin=(MainW-60-columnCount*appW)/(2);
        NSArray*title=[NSArray arrayWithObjects:MyString(@"养生"),MyString(@"祈愿"),MyString(@"供养"),MyString(@"经销商申请"),MyString(@"入驻申请"),MyString(@"寺院洽谈"), nil];
        NSArray*img=[NSArray arrayWithObjects:@"icon_health",@"icon_prayer",@"icon_fend",@"icon_dealer",@"icon_application",@"icon_temple", nil];
        
        for (int i=0; i<6; i++) {
            
            FL_Button *btn=[[FL_Button alloc] initWithAlignmentStatus:FLAlignmentStatusTop];
            [btn setTitle:title[i] forState:UIControlStateNormal];
            [btn setBackgroundImage:[UIImage imageNamed:img[i]] forState:UIControlStateNormal];
            btn.titleLabel.font=FontAndStyle14;
            [btn setTitleColor:TextColor6 forState:UIControlStateNormal];
            btn.tag=1000+i;
            [btn addTarget:self action:@selector(topBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            //计算列号和行号
            int colX=i%columnCount;
            int rowY=i/columnCount;
            //计算坐标
            CGFloat appX=30+colX*(appW+appMargin);
            CGFloat appY=25+rowY*(40+appH);
            
            btn.frame=CGRectMake(appX, appY, appW, appH);
            
            [view addSubview:btn];
        }
        UIView*v=[[UIView alloc]initWithFrame:CGRectMake(0, MainW*0.4+200, MainW, 1)];
        v.backgroundColor=Gray_Color;
        [self addSubview:v];
        
    }
    return self;
}

-(void)sectionHeadClick:(UIButton *)sender{
    [_delegate sectionHeadClick:sender];
}
-(void)fourBtnClick:(UIButton *)sender{
    [_delegate fourBtnClick:sender];
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    [_delegate cycleScrollView:cycleScrollView didSelectItemAtIndex:index];
}
-(void)topBtnClick:(FL_Button *)sender{
    [_delegate topBtnClick:sender];
}
-(void)setGroupView:(NSArray *)array NSArray:(NSArray *)arry{
    CGFloat height;
    if (array.count==0) {
        height=195;
    }else{
        height=(((MainW-15)/3)-10)*(array.count/2)+200;
    }
    UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(0, MainW*0.4+200, MainW, height)];
    view1.backgroundColor=[UIColor whiteColor];
    [self.Firstview addSubview:view1];
    UIView*view2=[[UIView alloc]initWithFrame:CGRectMake(0, 0, MainW, 1)];
    view2.backgroundColor=Gray_Color;
    [view1 addSubview:view2];
    int totalColumns = 2;
    
    //       每一格的尺寸
    CGFloat cellW = MainW/2-7.5;
    CGFloat cellH = ((MainW-15)/3)-10;
    
    //    间隙
    CGFloat margin =(MainW - totalColumns * cellW) / (totalColumns + 1);
    
    //    根据格子个数创建对应的框框
    for(int index = 0; index< array.count; index++) {
        UIImageView*img=[[UIImageView alloc]init];
        [img sd_setImageWithURL:[NSURL URLWithString:array[index][@"imageUrl"]] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
        UIButton *btn=[[UIButton alloc] init];
        [btn setTag:500+index];
        [btn addTarget:self action:@selector(fourBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        // 计算行号  和   列号
        int row = index / totalColumns;
        int col = index % totalColumns;
        //根据行号和列号来确定 子控件的坐标
        CGFloat cellX = margin + col * (cellW + margin);
        CGFloat cellY = 5+row * (cellH + margin);
        btn.frame = CGRectMake(cellX, cellY, cellW, cellH);
        img.frame=CGRectMake(cellX, cellY, cellW, cellH);
        [view1 addSubview:img];
        // 添加到view 中
        [view1 addSubview:btn];
    }

    UIImageView*bgimag=[[UIImageView alloc]initWithFrame:CGRectMake(0, height-190, MainW, 53)];
    //bgimag.image=[UIImage imageNamed:@"home_title_bg"];
    [view1 addSubview:bgimag];
    NSString*str=@"热门去";
    CGSize size=[str sizeWithAttributes:@{NSFontAttributeName:FontAndStyle14 }];
    self.nameLabel=[[MYLabel alloc]initWithFrame:CGRectMake(bgimag.bsg_centerX-size.width/2, 10, size.width, 15)];
    self.nameLabel.text=@"热门区";
    self.nameLabel.textAlignment=1;
    self.nameLabel.textColor=button_Red_Color;
    self.nameLabel.backgroundColor=[UIColor clearColor];
    UIImageView*rightImg=[[UIImageView alloc]initWithFrame:CGRectMake(self.nameLabel.bsg_x-36, self.nameLabel.bsg_centerY-4, 26, 7)];
    rightImg.image=[UIImage imageNamed:@"img_title"];
    [bgimag addSubview:rightImg];
    UIImageView*leftImg=[[UIImageView alloc]initWithFrame:CGRectMake(self.nameLabel.bsg_right+10, self.nameLabel.bsg_centerY-4, 26, 7)];
    leftImg.image=[UIImage imageNamed:@"img_title"];
    [bgimag addSubview:leftImg];
    self.smallTitle=[[MYLabel alloc]initWithFrame:CGRectMake(0, self.nameLabel.bsg_bottom+7, MainW, 10)];
    self.smallTitle.font=[UIFont systemFontOfSize:10];
    self.smallTitle.textAlignment=1;
    self.smallTitle.text=@"八风吹不动,端坐紫金莲";
    self.smallTitle.textColor=Red_Color;
    [bgimag addSubview:self.smallTitle];
    UIImageView*bomm=[[UIImageView alloc]initWithFrame:CGRectMake(self.smallTitle.bsg_centerX-100, self.smallTitle.bsg_bottom+3, 200, 7)];
    //bomm.image=[UIImage imageNamed:@"title_lines"];
    bgimag.backgroundColor=[UIColor whiteColor];
    [bgimag addSubview:bomm];
    self.iconImag=[[UIImageView alloc]initWithFrame:CGRectMake(leftImg.bsg_right+15, 14, 18, 26)];
    self.iconImag.image=[UIImage imageNamed:@"title_img_hot"];
    [bgimag addSubview:self.iconImag];
    UIButton *sectionBtn=[[UIButton alloc]initWithFrame:CGRectMake(MainW-130, height-200, 130, 53)];
    sectionBtn.tag=800;
    //    [sectionBtn setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [sectionBtn addTarget:self action:@selector(sectionHeadClick:) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *moreImg=[[UIImageView alloc]initWithFrame:CGRectMake(MainW-50, 18, 40, 14)];
    moreImg.image=[UIImage imageNamed:@"more"];
    [bgimag addSubview:self.nameLabel];
    [bgimag addSubview:moreImg];
    [view1 addSubview:sectionBtn];
    
    self.sectionScrollerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, bgimag.bsg_bottom, MainW, MainW*0.4) delegate:self placeholderImage:[UIImage imageNamed:@"banner_bj"]];
    self.sectionScrollerView.tag=101;
    //    for (scrollViewModel * model in self.scrollViewArray) {
    //        NSString * pic = [NSString stringWithFormat:@"%@%@",baseImgUrl,model.banner_img];
    //        [array addObject:pic];
    //    }

 self.sectionScrollerView.bannerImageViewContentMode=UIViewContentModeScaleAspectFill;
    self.sectionScrollerView.imageURLStringsGroup = arry;  // 网络图片
    //self.sectionScrollerView.localizationImageNamesGroup =array1 ; // 本地图片
    self.sectionScrollerView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    self.sectionScrollerView.autoScrollTimeInterval =3.;
    [view1 addSubview:self.sectionScrollerView];
    [self addSubview:view1];

}
@end
